package com.cp.weixin.blog.controller;

import com.cp.weixin.blog.repository.Contact;
import com.cp.weixin.entity.Result;
import com.cp.weixin.blog.service.ContactService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class ContactController {

    Logger log = LogManager.getLogger();

    @Autowired
    ContactService contactService;

    @GetMapping("/contact.html")
    public String contactUs(){
        return "/contact/contactUs.html";
    }

    @RequestMapping(value = "/addContact")
    @ResponseBody
    public Result contact(Contact contact, Model model) {
        Result result = new Result();
        try {
            int out = contactService.addContact(contact);
            if(out==1){
                result.setState(true);
                result.setMessage("success");
            } else {
                result.setState(false);
                result.setMessage("fail");
            }
        } catch(Exception ex){
            log.error(ex.getMessage());
            result.setState(false);
            result.setMessage(ex.getMessage());
        }
        return result;
    }


}
